/*----------------------------------------------------------\\
|| Mancala                                                  ||
|| CS-340 with Professor Troy                               ||
|| Joshua Jung and Eugene Magdel                            ||
|| April 29, 2011                                           ||
\\----------------------------------------------------------*/
#include "board.h"
//Initializes board
Board::Board() : players_turn(0) {
    Cup::current = NULL;
    //TODO move this somewhere into main when integrated with QT
    srand (time(NULL));
    my_player_number = 0;
    opponent_player_number = 1;
    cup = new Cup(0, 1);
    Cup * first_cup = cup;
    for(int i=1; i<14; ++i){
        int player_number = (i-1) / 7;
        Cup * newly_created = new Cup(i, player_number);
        cup -> set_next(newly_created);
        cup = cup -> get_next();
        //DEBUGGING
        // cout << "Cup value is: " << cup -> get_index();
        //  cout << " Gem count is: " << cup -> gem_count();
        if(cup -> is_mancala()){
            //   cout << " and is mancala";
        }
        //  cout << endl;
    }
    // cout << "Connecting last cup to first" << endl;
    cup -> set_next(first_cup);
    //cout << "Success connecting last cup to first" << endl;
    Cup * current_cup = cup;
    // cout << "setting up board finished" << endl;
}
void Board::toggle_turn(){
    if (players_turn == 0)
        players_turn = 1;
    else
        players_turn = 0;
}

void Board::set_turn(int turn){

    players_turn = turn;

}

//Moves pieces on the board
//Returns true if same player has another move
bool Board::move(int x, int player_for){

    if (player_for == -1)
    {
        player_for = players_turn;
    }

    Cup &clicked_cup = Cup::cup_at(x);

    // Verify clicked cup belongs to the user requesting the move
    if((clicked_cup.get_owner() == player_for && clicked_cup.gem_count() > 0))
    {
        Cup *next_cup = &clicked_cup;

        // Transfer gems one at a time to following cups.
        while(!clicked_cup.empty())
        {
            next_cup = next_cup -> get_next();
            next_cup -> add_gem(clicked_cup);
        }

        //Transfer gems from other player cup to your if yours has one gem
        if(next_cup -> gem_count() == 1 && !next_cup -> is_mancala() && next_cup -> get_across() .gem_count() > 0 && next_cup -> get_owner() == player_for)
        {
            Cup & player_mancala = Cup::get_mancala_for(player_for);

            player_mancala.add_gems(next_cup -> get_across());

            player_mancala.add_gems(*next_cup);
        }
        // Return true if the user gets another turn because the last cup they entered was their own mancala
        return next_cup -> is_mancala() && next_cup -> get_owner() == player_for;
    }

    return true;
}

//Computes score for given player number
int Board::get_scores(int player_number){
    int score = 0;
    Cup* current_cup = cup;
    do{
        if(current_cup -> get_owner() == player_number){
            score += current_cup -> gem_count();
        }
        current_cup = current_cup -> get_next();
    }while(current_cup != cup);
    return score;
}

//returns player number if won or -1 if no winner yet
int Board::won_by(){
    //Check first set of cups
    Cup * init_cup = &Cup::cup_at(1);
    bool empty = true;
    for(int i = 1; i < 7; i++){
        if(!init_cup -> empty()){
            empty = false;
            break;
        }
        init_cup = init_cup -> get_next();
    }
    if(empty)
        return 0;
    //Check second set of cups
    init_cup = &Cup::cup_at(8);
    empty = true;
    for(int i = 8; i < 14; i++){
        if(!init_cup -> empty()){
            empty = false;
            break;
        }
        init_cup = init_cup -> get_next();
    }
    if(empty)
        return 1;
    return -1;
}

int Board::winner_is(){
    int player_0_score = get_scores(0);
    int player_1_score = get_scores(1);

    if(won_by() != -1){
      //Move all gems to owners mancala
        Cup *target_cup = &Cup::cup_at(1);
        Cup *player_mancala = &Cup::get_mancala_for(0);
        while(!target_cup -> is_mancala()){
          player_mancala -> add_gems(*target_cup);
          target_cup = target_cup -> get_next();
        }
        target_cup = &Cup::cup_at(8);
        player_mancala = &Cup::get_mancala_for(1);
        while(!target_cup -> is_mancala()){
          player_mancala -> add_gems(*target_cup);
          target_cup = target_cup -> get_next();
        }
        return player_0_score < player_1_score ? 1 : 0;
    }
    return -1;
}

void Board::print_board(){
    cout << "\n\n\nPRINTING BOARD" << endl;
    Cup * first_cup = &Cup::cup_at(0);
    //DEBUGGING
    Cup * next_cup = first_cup;
    do{
        string cup_type = "cup: ";
        if(next_cup -> is_mancala())
            cup_type = "mancala: ";
        cout << cup_type << next_cup -> get_index() << " Has: " << next_cup -> gem_count();
        //cout << " Gems are: " << next_cup -> gem_images() << endl;
        cout << endl;
        next_cup = next_cup -> get_next();
    }
    while(first_cup != next_cup);

}
